﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OSGeo.MapGuide.ObjectModels;

namespace OSGeo.MapGuide
{
    /// <summary>
    /// Defines a MapGuide feature join
    /// </summary>
    public sealed class MgJoinDefinition
    {
        /// <summary>
        /// Creates a new instance of <see cref="MgJoinDefinition"/>
        /// </summary>
        /// <param name="left">The primary or "left" feature source</param>
        /// <param name="right">The secondary or "right" feature source</param>
        /// <param name="joinResId">The resource id where the join definition will be saved to</param>
        /// <param name="extendedFeatureClassName">The name of the extended feature class. If blank will default to [class]Extended</param>
        /// <param name="joinProperties">The list of properties to join on. The <see cref="Key"/> property represents the property name on the left side and the <see cref="Value"/> property represents the property name on the right side.</param>
        public MgJoinDefinition(MgJoinPrimaryFeatureSource left, MgJoinSecondaryFeatureSource right, MgResourceIdentifier joinResId, string extendedFeatureClassName, MgStringPair[] joinProperties)
        {
            if (null == left)
                throw new ArgumentNullException("left");

            if (null == right)
                throw new ArgumentNullException("right");

            if (null == joinResId)
                throw new ArgumentNullException("joinResId");

            if (null == joinProperties || joinProperties.Length == 0)
                throw new ArgumentException("Array is null or empty", "joinProperties");

            this.Left = left;
            this.Right = right;
            this.JoinResourceId = joinResId;
            this.JoinProperties = joinProperties;

            if (!string.IsNullOrEmpty(extendedFeatureClassName))
                this.ExtendedClassName = extendedFeatureClassName;
            else
                this.ExtendedClassName = this.Left.ClassName.Trim() + "Extended";

            this.JoinType = RelateTypeEnum.LeftOuter;
            this.Cardinality = MgJoinCardinalityType.OneToMany;
        }

        /// <summary>
        /// Gets or sets whether to force a 1:1 join, only applies if the cardnality is 1:many
        /// </summary>
        public bool ForceOneToOne { get; set; }

        /// <summary>
        /// Gets the join properties
        /// </summary>
        public MgStringPair[] JoinProperties { get; private set; }

        /// <summary>
        /// Gets the extended feature class name
        /// </summary>
        public string ExtendedClassName { get; private set; }

        /// <summary>
        /// Gets the primary (or left) feature source
        /// </summary>
        public MgJoinPrimaryFeatureSource Left { get; private set; }

        /// <summary>
        /// Gets the secondary (or right) feature source
        /// </summary>
        public MgJoinSecondaryFeatureSource Right { get; private set; }

        /// <summary>
        /// Gets the resource id where the join definition content will be saved to
        /// </summary>
        public MgResourceIdentifier JoinResourceId { get; private set; }

        /// <summary>
        /// Gets or sets the join type
        /// </summary>
        public RelateTypeEnum JoinType { get; set; }

        /// <summary>
        /// Gets or sets the cardinality of the join
        /// </summary>
        public MgJoinCardinalityType Cardinality { get; set; }
    }
}
